﻿using System;
using System.Collections.Generic;
using System.Text;

namespace GISCommon.Business
{
    public class MeasureToolsHelper2
    {
        ESRI.ArcGIS.Controls.AxMapControl m_MapControl = null;
        ESRI.ArcGIS.Geometry.IPoint m_prePoint = null;
        ESRI.ArcGIS.Geometry.IPoint m_curPoint = null;
        double m_distance = 0;
        System.Windows.Forms.Label m_lbl_Text = new System.Windows.Forms.Label();
        bool m_IsStarted = false;
        bool m_IsCalled = false;
        ESRI.ArcGIS.Geometry.IGeometryCollection m_GeoColl = new
            ESRI.ArcGIS.Geometry.PolylineClass();

        public bool StartMeasure()
        {
            try
            {
                m_IsStarted = true;
                
                return true;
            }
            catch (Exception ex)
            {
                SysCommon.Global.mainLog.Error("调用StartMeasure()出错", ex);
                return false;
            }
        }

        public bool StopMeasure()
        {
            try
            {
                m_IsStarted = false;
                m_IsCalled = false;
                
                //Tools.Drawing.ExitDrawPolyline();
                return true;
            }
            catch (Exception ex)
            {
                SysCommon.Global.mainLog.Error("调用StartMeasure()出错", ex);
                return false;
            }
        }

        public void Initial(ESRI.ArcGIS.Controls.AxMapControl vMapControl)
        {
            try
            {
                
                m_MapControl = vMapControl;

                m_MapControl.OnMouseUp += new ESRI.ArcGIS.Controls.IMapControlEvents2_Ax_OnMouseUpEventHandler(m_MapControl_OnMouseUp);
                m_MapControl.OnMouseMove += new ESRI.ArcGIS.Controls.IMapControlEvents2_Ax_OnMouseMoveEventHandler(m_MapControl_OnMouseMove);
                m_MapControl.OnDoubleClick += new ESRI.ArcGIS.Controls.IMapControlEvents2_Ax_OnDoubleClickEventHandler(m_MapControl_OnDoubleClick);


            }
            catch (Exception ex)
            {
                SysCommon.Global.mainLog.Error("调用Initial函数出错", ex);
            }
        }

        void m_MapControl_OnDoubleClick(object sender, ESRI.ArcGIS.Controls.IMapControlEvents2_OnDoubleClickEvent e)
        {
            try
            {
                if (!m_IsStarted) return;
                m_lbl_Text.Visible = false;
                double dis = m_distance * 28.2 * 3600;
                System.Windows.Forms.MessageBox.Show("量算长度结果：" + dis.ToString("0000") + "(米)");
                m_MapControl.ActiveView.PartialRefresh(ESRI.ArcGIS.Carto.esriViewDrawPhase.esriViewGraphics, null, null);
                m_distance = 0;
                m_curPoint = null ;
                m_prePoint = null;
                m_IsCalled = false;
            }
            catch (Exception ex)
            {
                SysCommon.Global.mainLog.Error("调用出错", ex);
            }
        }

        void m_MapControl_OnMouseMove(object sender, ESRI.ArcGIS.Controls.IMapControlEvents2_OnMouseMoveEvent e)
        {
            try
            {
                if (!m_IsStarted || !m_IsCalled) return;
                ESRI.ArcGIS.Geometry.IPoint pt = new ESRI.ArcGIS.Geometry.PointClass();
                pt.PutCoords(e.mapX, e.mapY);

                if (m_prePoint != null)
                {
                    double tmpDistance = m_MapControl.Map.ComputeDistance(m_prePoint, pt);

                    double distance = (tmpDistance + m_distance) * 28.3465 * 3600;

                    this.DrawText(e.x,e.y, "当前总长度：" + distance.ToString("0") + "(米)");

                    pt = null;
                }
            }
            catch (Exception ex)
            {
                SysCommon.Global.mainLog.Error("调用m_MapControl_OnMouseMove出错", ex);
            }
        }

        void m_MapControl_OnMouseUp(object sender, ESRI.ArcGIS.Controls.IMapControlEvents2_OnMouseUpEvent e)
        {
            try
            {
                if (!m_IsStarted) return;
                if (!m_IsCalled)
                {
                    //Tools.Drawing.DrawPolyline(m_MapControl);
                    //Tools.Drawing.DrawPolyline(
                    m_IsCalled = true;
                }
                if (m_prePoint == null)
                {
                    m_prePoint = new ESRI.ArcGIS.Geometry.PointClass();
                    m_prePoint.PutCoords(e.mapX, e.mapY);
                    return;
                }
                else
                {
                    if (null == m_curPoint) m_curPoint = new ESRI.ArcGIS.Geometry.PointClass();
                    m_curPoint.PutCoords(e.mapX, e.mapY);
                    ESRI.ArcGIS.Geometry.IPolyline polyline =
                        new ESRI.ArcGIS.Geometry.PolylineClass();
                    polyline.SpatialReference = m_MapControl.SpatialReference;
                    polyline.FromPoint = m_prePoint;
                    polyline.ToPoint = m_curPoint;
                    object obj = System.Reflection.Missing.Value;

                    //m_GeoColl.AddGeometry((ESRI.ArcGIS.Geometry.IGeometry)polyline,
                    //    ref obj, ref obj);
                    GISCommon.Tools.Drawing.DrawPolyline(m_MapControl, (ESRI.ArcGIS.Geometry.IGeometry)polyline);

                    m_distance += m_MapControl.Map.ComputeDistance(m_prePoint, m_curPoint);
                    m_prePoint = m_curPoint;
                }
            }
            catch (Exception ex)
            {
                SysCommon.Global.mainLog.Error("调用m_MapControl_OnMouseUp出错", ex);
            }
        }

        void DrawText(int x,int y, string text)
        {
            try
            {
                System.Drawing.Point point = new System.Drawing.Point(x, y);
                m_lbl_Text.Location = point;
                m_lbl_Text.Text = text;
                m_lbl_Text.Visible = true;
            }
            catch (Exception ex)
            {
                SysCommon.Global.mainLog.Error("调用DrawText(ESRI.ArcGIS.Geometry.IPoint point, string text)出错", ex);
            }
        }
    }
}
